<template>
  <div>
    <el-card>
      <div slot="header" class="clearfix">
        <el-row :gutter="10" style="display: flex;align-items: center;">
          <el-col :span="6">
            <h3>搜索文章 '{{params.searchKey}}' 所有结果</h3>
          </el-col>
        </el-row>
      </div>
      <div v-if="blogList.length">
        <ArticleList
          v-for="(blog, index) in blogList"
          :key="index"
          :article-data="blog"
        />
      </div>
      <div v-else> <el-empty description="没有搜索到相关文章或者文章作者已被您拉黑！"/></div>
    </el-card>
    <el-pagination
      v-if="total > params.limit"
      layout="prev, pager, next"
      :total="total"
      :page-size="params.limit"
      @current-change="changePage"
    />
  </div>
</template>

<script>
import ArticleList from '@/components/ArticleList'
import BlogService from '@/service/blogs/BlogService'

export default {
  name: "BlogSearched",
  components:{
    ArticleList
  },
  data(){
    return {
      blogList: [],
      params: {
        limit: 1,
        offset: 0,
        searchKey: ''
      },
      total: 0
    }
  },
  created() {
    this.params.searchKey = this.$route.params.searchKey
    this.getBlogData()
  },
  methods:{
    async getBlogData(){
      const res = await BlogService.getPublicBlogs(this.params)
      this.total = res.data.data.blogTotal
      this.blogList = res.data.data.blogList
    },
    changePage(page){
      this.params.offset = (page - 1) * this.params.limit
      this.getBlogData()
    }
  }
}
</script>
